namespace LuUpgrade.Utils
{
    public class ConfigManager(string configFilePath)
    {
        private readonly Dictionary<string, string> _config = ReadIniFile(configFilePath);

        public string GetConfigValue(string key)
        {
            if (_config.TryGetValue(key, out var value))
            {
                return value;
            }

            throw new KeyNotFoundException($"配置中不存在键: {key}");
        }

        private static Dictionary<string, string> ReadIniFile(string filePath)
        {
            var config = new Dictionary<string, string>();

            // 读取文件
            var lines = File.ReadAllLines(filePath);

            foreach (var line in lines)
            {
                // 忽略空行和注释行
                if (string.IsNullOrWhiteSpace(line) || line.StartsWith(";"))
                    continue;

                // 解析键值对
                var parts = line.Split('=');
                if (parts.Length >= 2)
                {
                    // 将键和值添加到字典中
                    var key = parts[0].Trim();
                    var value = parts[1].Trim();
                    config[key] = value;
                }
            }

            // string json = JsonConvert.SerializeObject(config);
            // Console.WriteLine("数据库字典");
            // Console.WriteLine(json);

            return config;
        }
    }
}